define(['app','js/directive/listTree','js/filter/tree2Arr'], function( app ) {
    'use strict';
    /*岗位管理*/
    app.controller('Role', function ($scope, dataFactoryCRM , $q, $timeout) {
    	$scope.roles;
    	$scope.selected;
    	$scope.select;
        $scope.add_data = {role_name:'',parent_role:-1,sector_id:-1,info:'',rights:[]};
        $scope.edit_data = {role_id:0,role_name:'',parent_role:-1,sector_id:-1,info:'',rights:[]};
    	$scope.pageParam = {sectors:[],rights:[]};

    	$scope.datalist = function(){
            $scope.busy = dataFactoryCRM.getDataByAPI('Role/index').then(
                    function( d ){
                        $scope.roles = d.data;
                    });
            }


        $scope.initParam = function(){
            dataFactoryCRM.getDataByAPI('sector/index').then(
                function( d ){
                    $scope.pageParam.sectors = d.data
                });
            dataFactoryCRM.getDataByAPI('right/index').then(
                function( d ){
                    $scope.pageParam.rights = d.data
                });
        }


        $scope.addchk = function( index ){
            var vr =  $scope.pageParam.rights[index];
            var k = $scope.add_data.rights.indexOf( vr.right_id );
            if( k === -1 )
                $scope.add_data.rights.push( vr.right_id );
            else
                $scope.add_data.rights.splice(k,1);

        }

        $scope.editchk = function( index ){
            var vr =  $scope.pageParam.rights[index];
            var k = $scope.edit_data.rights.indexOf( vr.right_id );
            if( k === -1 )
                $scope.edit_data.rights.push( vr.right_id );
            else
                $scope.edit_data.rights.splice(k,1);

        }

        $scope.showAdd = function(){
            function addRole( dataRole ){
                $scope.busy = dataFactoryCRM.postDataByAPI('role/add', dataRole )
                .then(function(d) {
                  if ( d && d.rcode) {
                        $scope.add_data = {role_name:'',parent_role:-1,sector_id:-1,info:'',rights:[]};
                        $scope.datalist();
                        $('.ui.role.add.modal').modal('hide');
                  } else {
                    alert( d.msg );
                  }
                });
            }

            $('.ui.role.add input').val('');
            $('#frm-add .ui.basic.red').remove();
            $('.ui.dropdown').dropdown('restore defaults');
            $('input[name="add[parent_role]"]').parent().dropdown('set selected',  $scope.user.role_id );
            $('input[name="add[sector_id]"]').parent().dropdown('set selected', $scope.user.sector_id );
            $scope.add_data.sector_id = $scope.user.sector_id;
            $scope.add_data.parent_role = $scope.user.role_id;
            $('.ui.role.add.modal').modal({
                onApprove: function() {
                    if(  $('#frm-add').form('is valid') ){
                        $scope.add_data.parent_role = $('input[name="add[parent_role]"]').val();
                        $scope.add_data.sector_id = $('input[name="add[sector_id]"]').val();
                        addRole( $scope.add_data );
                    }
                    return false;
                }
            }).modal('show');
        }

        $scope.nodeEdit = function( obj ){
             function editRole( dataRole ){
                $scope.busy = dataFactoryCRM.postDataByAPI('role/edit', dataRole )
                .then(function(d) {
                  if( d && d.rcode) {
                        $scope.edit_data = {};
                        $scope.datalist();
                        $('.ui.role.edit.modal').modal('hide');
                  }else{
                    alert( d.msg );
                  }
                });
            }

            $scope.edit_data = {};
            $("input[name='edit[rights][]']").each(function(){  $(this).prop('checked', false); });
            $scope.busy = dataFactoryCRM.getDataByAPI('role/edit?role_id='+ obj.role_id)
                .then(function(d) {
                  if (d.rcode) {
                        $scope.edit_data =  d.data;
                        if( $scope.edit_data && $scope.edit_data.rights ){
                            var vchk = 0;
                            var varr = $scope.edit_data.rights;
                            $("input[name='edit[rights][]']").each(function(){
                                vchk = parseInt( $(this).val() );
                                if( varr.indexOf( vchk) >=0 ){
                                    $(this).prop('checked',true);
                                }
                            });
                        }
                        $('input[name="edit[parent_role]"]').parent().dropdown( 'set selected',$scope.edit_data.parent_role  );
                        $('input[name="edit[sector_id]"]').parent().dropdown('set selected', $scope.edit_data.sector_id );
                  } else {
                    alert( d.msg );
                    return false;
                  }
            });

            $('.ui.dropdown').dropdown('restore defaults');
            $('.ui.role.edit.modal').modal({
                onApprove: function() {
                    if( $('#frm-edit').form('is valid') ){
                        $scope.edit_data.parent_role = $('input[name="edit[parent_role]"]').val();
                        $scope.edit_data.sector_id = $('input[name="edit[sector_id]"]').val();
                        $scope.edit_data.rights = [];
                        $("input[name='edit[rights][]']").each(function(){
                                if(  $(this).prop('checked') )
                                    $scope.edit_data.rights.push( parseInt( $(this).val() ) );
                            });
                        editRole( $scope.edit_data );
                    }
                    return false;
                }
            }).modal('show');
        }

    	$scope.initParam();
        $scope.datalist();
    }).filter(
        //过滤器，用于让文本中的HTML生效
        'to_trusted', ['$sce', function ($sce) {
            return function (text) {
                return $sce.trustAsHtml(text);
            }
        }]
    );
});